/**	
 * <br>
 * Copyright 2011 IFlyTek. All rights reserved.<br>
 * <br>			 
 * Package: com.iflytek.sjzx.xmgl.dao <br>
 * FileName: ProjectManagerDao.java <br>
 * <br>
 * @version 0.1
 * @author GUOZHEN
 * @created 2013-10-22
 */

package com.iflytek.ci.project.dao;

import com.iflytek.ci.base.SysCode;
import com.iflytek.ci.common.AnalyseHqlMaker;
import com.iflytek.ci.common.StringUtil;
import com.iflytek.ci.project.domain.Project;
import com.iflytek.iframework.orm.Page;
import com.iflytek.iframework.orm.hibernate.HibernateEntityDao;
import com.iflytek.iframework.utils.HqlMaker;
import org.hibernate.HibernateException;
import org.hibernate.Query;

/**
 *  Prject Manager module's Dao.
 *  
 *  @author yali
 */

public class ProjectManagerDao extends HibernateEntityDao<Project> {


    /**
     * Convert id string collection to Long type collection.<br />
     *      This method is used for convert id string collection(eg:"1,2,3") like the string seperated by comma to an
     *      Long type collection(type:Long [],eg:Long []{1,2,3}),for batch operation with id collection.
     * @param ids
     *         The id collection string seperated by comma.eg "1,2,3".
     * @return
     *         The Long [] type collection for id . eg:Long [] {1,2,3}
     */
    protected Long [] convertStringToLongArray(String ids){
        String [] idstr = ids.split(SysCode.SPECIAL_CHARACTER.SEPARATOR_DH);
        Long []  idList = new Long[idstr.length];
        for (int i=0;i<idstr.length;i++){
            idList[i] = Long.valueOf(idstr[i]);
        }
        return idList;
    }

    /**
     * 
     *  <p>
     *      Query the projects list with pagination and some other query conditions,like projectName,resourceType.etc.
     *  </p>
     *  @param page
     *          The pagination condition.
     *  @param xm
     *          Other base query condition package with the project object.
     *          eg:
     *            projectName : project's name (support Fuzzy query)
     *            resourceType : the code of the poject's type.
     *            ownerSystem : the system's id which this project subordinated.
     *            etc.
     *  @author yali
     *  @return
     *          The query result with pagination.
     */
     public final Page<Project> pageProjectWithQueryCondition(Page<Project> page,Project xm){
        StringBuilder hql = new StringBuilder(AnalyseHqlMaker.popuHqlFrom("Project"));
        hql.append(AnalyseHqlMaker.popuHqlLikeForEscape("projectName", StringUtil.replace(xm.getProjectName())));
        hql.append(HqlMaker.popuHqlEq("jzCategory", xm.getJzCategory()));
        hql.append(HqlMaker.popuHqlEq("resourceType", xm.getResourceType()));
        return super.findPage(page, hql.toString());
    }

    /**
     * Create a new Project.
     *
     * @param project
     *          The project object with no objectid.
     * @return
     *          The new project's id.(exception with error code)
     */
    public final Long createNewProject(Project project){
         saveOrUpdate(project);
         return project.getObjectid()==null ? project.getObjectid() : SysCode.OPERATION_STATE.ERROR_CODE;
    }

    /**
     * Query project with it's id.
     *
     * @param id
     *          The project's id.
     * @return
     *          The project object.
     */
    public final Project getProjectWithId(String id){
        return findUniqueBy("objectid", Long.valueOf(id));
    }

    /**
     * Change the projects' state with id collection string seperated with comma.
     *
     * @param ids
     *          The prjects' id collection string.eg:"1,2,3,4".
     * @param state
     *          The state value to be setted.
     * @return
     *          The number of records been updated..
     */
    public final int changeStateByIds(String ids,String state){
        String sql = "update Project set state = :state where objectid in (:ids)";
        Query query = createQuery(sql).setParameter("state", state).setParameterList("ids", convertStringToLongArray(ids));
        return query.executeUpdate();
    }

    /**
     * Update the base information of project.
     *
     * @param project
     *          The project's object to be updated.
     * @return
     *          The update operation result.eg 0 success, -1 error
     */
    public final int updateProject(Project project){
         try {
             saveOrUpdate(project);
             return SysCode.OPERATION_STATE.SUCCESS_CODE;
         } catch (HibernateException exception){
            return SysCode.OPERATION_STATE.ERROR_CODE;
         }
    }

    /**
     * Delete the projects with the id collection.
     *
     * @param ids
     *
     * @return
     *         The records number been deleted.
     */
    public final int deleteProjects(String ids){
         String sql = "delete from  Project where objectid in (:ids)";
         Query query = createQuery(sql).setParameterList("ids",convertStringToLongArray(ids));
         return query.executeUpdate();
    }

}




